// 共用判断处理函数
const dealSucc = (scope) => {
  return (res, authSucc, authFail) => {
    if (res.authSetting[scope]) {
      // 授权成功
      authSucc();
    } else {
      // 授权失败
      authFail(scope, 'reject');
    }
  }
}
/**
 * 查询授权情况
 */
const getAuth = (succ, authFn, authFail, dealFn) => {
  wx.getSetting({
    success: res => {
      console.log(res);
      succ(res, authFn, authFail);
    },
    fail: err => {
      dealFn(null, 'ask');
    }
  });
}
/**
 * 打开授权弹窗
 */
const auth = (succ, fail, showModel) => {
  return (scope) => {
    wx.authorize({
      scope: scope,
      success() {
        succ();
      },
      fail() {
        if (typeof showModel === 'function') {
          showModel(fail);
        } else {
          fail();
        }
      }
    });
  }
}
/**
 * 打开授权列表接口
 */
const openAuthList = (succ, auth, dealFn) => {
  return () => {
    wx.openSetting({
      success: res => {
        succ(res, auth, dealFn);
      },
      fail: err => {
        console.log(err, 888);
        dealFn(null, 'reject');
      }
    })
  }
}
/**
 * 共用初始化调用
 * @param getData 要调用的权限接口
 * @param type    授权类型
 * @param cb      授权回调
 */
const init = (getData, type, cb, showModel) => {
  // const showModel = (fn) => {
  //   wx.showModal({
  //     title: "",
  //     content: "你已经拒绝地址授权，是否去打开？",
  //     success: res => {
  //       if (res.confirm) {
  //         fn();
  //       } else {
  //         cb(null, 'reject');
  //       }
  //     }
  //   })
  // }
  const succ = dealSucc(type);
  const getD = getData(cb);
  const openAuth = openAuthList(succ, getD, cb);
  // 授权管理
  const authFn = auth(getD, openAuth, showModel);
  //
  getAuth(succ, getD, authFn, cb);
}

/**
 * 弹窗提示
 *
 */
const showModal = (cb, tips) => {
  return (fn) => {
    wx.showModal({
      title: "",
      content: tips,
      success: res => {
        if (res.confirm) {
          fn();
        } else {
          cb(null, 'reject');
        }
      }
    })
  }
}

/**
 * 地址授权接口
 * @param func 授权回调
 */
const address = (func) => {
  /**
   * 选择地址接口
   */
  const chosseAddress = (dealFunc) => {
    return () => {
      wx.chooseAddress({
        success: res => {
          // console.log(res, 789);
          dealFunc(res, 'compelte')
        },
        fail: err => {
          // console.log('不选择地址', 4444);
          dealFunc(err, 'authSucc')
        }
      })
    }
  }
  const model = showModal(func, '你已经拒绝地址授权，是否去打开？');

  init(chosseAddress, 'scope.address', func, model);
}

/**
 * 获取保存到相册权限
 * @type {{address: function(*=), userInfo: function(*=)}}
 */
const savePic = (func, res) => {
  // 保存相册权限
  const saveAlbum = (dealFunc) => {
    return () => {
      // dealFunc(res, 'compelte');
      wx.saveImageToPhotosAlbum({
        filePath: res,
        success: res => {
          // debugger;
          dealFunc(res, 'compelte')
        },
        fail: err => {
          // debugger;
          dealFunc(err, 'authSucc')
        }
      })
    }
  }
  const model = showModal(func, '你已经拒绝保存到相册，是否去打开？');
  init(saveAlbum, 'scope.writePhotosAlbum', func, model);
}

/**
 * 用户信息授权
 *
 */
const userInfo = (func, dealFn) => {
  // 获取用户信息
  const getUserMess = (dealFunc) => {
    return () => {
      wx.getUserInfo({
        success: res => {
          dealFunc(res, 'compelte')
        },
        fail: err => {
          dealFunc(err, 'authSucc')
        }
      })
    }
  }
  const model = showModal(func, '你已经拒绝获取用户信息授权，是否去打开？');
  init(getUserMess, 'scope.userInfo', func, model);
}

exports.auth = {address, savePic, userInfo};